<template>
  <div>
    <a href="/">返回首页</a>
    <br />
    <h1>作业——公告的增删改查(管理员版)</h1>
  </div>

  <!-- {{ list }} -->
  <!-- {{ page }}
  {{ queryInfo }} -->

  <!-- 数据loading部分 -->
  <div v-loading="loading">
    <!-- 搜索部分 -->
    <div style="display: flex; justify-content: flex-end; margin: 1rem">
      <el-form :inline="true">
        <el-form-item>
          <el-input
            v-model="queryInfo.title"
            placeholder="标题模糊查询"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-input
            v-model="queryInfo.content"
            placeholder="内容模糊查询"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-input
            v-model="queryInfo.username"
            placeholder="发布人模糊查询"
          ></el-input>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item>
          <el-button-group>
            <el-button type="danger" @click="btnReset">重置</el-button>
            <el-button type="primary" @click="btnSearch">查询</el-button>
            <el-button type="success" @click="btnAdd">添加</el-button>
          </el-button-group>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格部分 -->
    <div>
      <el-table :data="list">
        <el-table-column
          label="编号"
          prop="bid"
          width="160px"
        ></el-table-column>

        <el-table-column label="标题" prop="title"></el-table-column>

        <el-table-column label="发布人" prop="username"></el-table-column>

        <el-table-column label="是否置顶" prop="topIndex"></el-table-column>

        <el-table-column
          label="公告发布时间"
          prop="lastupdate"
          #default="scope"
        >
          {{ tools.formatDate(scope.row.lastupdate) }}</el-table-column
        >
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="danger" size="small" @click="del(scope.row)"
              >删除</el-button
            >

            <el-button
              type="primary"
              size="small"
              @click="showview(scope.row.bid)"
              >查看内容</el-button
            >

            <el-button
              type="warning"
              size="small"
              @click="showModify(scope.row.bid)"
              >修改</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页部分 -->
    <div style="display: flex; justify-content: center; margin-top: 1rem">
      <el-pagination
        v-model:current-page="page.pageNumber"
        :page-size="page.pageSize"
        :total="page.total"
        background
        layout="prev, pager, next ,total"
        @current-change="query"
      />
    </div>
  </div>

  <!-- 添加内容部分 -->
  <div>
    <el-dialog v-model="addDisplay" title="添加公告" @closed="btnReset">
      <!-- 添加内容的部分 -->
      <div>
        <el-form>
          <el-form-item>
            <el-input v-model="addInfo.title" placeholder="公告标题"></el-input>
          </el-form-item>

          <el-form-item>
            <el-input
              v-model="addInfo.username"
              placeholder="公告发布人"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-input
              type="textarea"
              rows="10"
              v-model="addInfo.content"
              placeholder="公告内容"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <!-- 底部的按钮 -->
      <template #footer>
        <el-button type="danger" @click="addDisplay = false">取消</el-button>
        <el-button type="primary" @click="add">确定发布</el-button>
      </template>
    </el-dialog>
  </div>

  <!-- 查看公告内容部分 -->
  <div>
    <el-dialog v-model="viewDisplay">
      <div class="viewinfo">
        <div>
          <h1>{{ viewcontent.title }}</h1>
        </div>
        <hr />
        <div v-html="viewcontent.content"></div>
        <hr />
        <div>
          <div>发布人：{{ viewcontent.username }}</div>
          <div>发布时间：{{ tools.formatDate(viewcontent.lastupdate) }}</div>
        </div>
      </div>
    </el-dialog>
  </div>

  <!-- 修改公告内容部分 -->
  <div>
    <el-dialog v-model="modifyDisplay" title="修改公告" @closed="query">
      <!-- 添加内容的部分 -->
      <div>
        <el-form>
          <el-form-item>
            <el-input
              v-model="modifyInfo.title"
              placeholder="公告标题"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-input
              v-model="modifyInfo.username"
              placeholder="公告发布人"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-input
              type="textarea"
              rows="10"
              v-model="modifyInfo.content"
              placeholder="公告内容"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <!-- 底部的按钮 -->
      <template #footer>
        <el-button type="warning" @click="modifyDisplay = false"
          >取消</el-button
        >
        <el-button type="success" @click="modify">确定修改</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { api } from '../../js/ajax'
import { tools } from '../../js/tools'
import { ElMessageBox, ElMessage } from 'element-plus'

const loading = ref(false)

//#region 主页面: 搜索部分、数据部分、分页部分

// 页面信息
const page = ref({
  pageNumber: 1,
  pageSize: 10,
})

// 获取数据
const list = ref([])

// 查询的数据收集
const queryInfo = ref({
  username: '',
  title: '',
  content: '',
})

// 获取数据的方法
const query = () => {
  loading.value = true
  api.get(
    '/crud/billboard',
    tools.mergeJson(queryInfo.value, page.value),
    data => {
      if (data.success) {
        setTimeout(() => {
          loading.value = false
        }, 50)
        list.value = data.data.data
        page.value = data.data.pageBean
        return
      }
      ElMessageBox.alert(data.message, '公告管理')
    },
  )
}
query()

// 查询按钮
const btnSearch = () => {
  page.value.pageNumber = 1
  query()
}

// 重置按钮
const btnReset = () => {
  page.value.pageNumber = 1
  queryInfo.value = {
    username: '',
    title: '',
    content: '',
  }
  query()
}

// 添加公告部分

// 不显示添加的对话框
const addDisplay = ref(false)

// 要添加的内容
const addInfo = ref({
  username: '',
  title: '',
  content: '',
})

// 点击添加按钮后
const btnAdd = () => {
  addInfo.value = {
    username: '',
    title: '',
    content: '',
  }
  addDisplay.value = true
}

// 添加功能
const add = () => {
  api.post('/crud/billboard', addInfo.value, data => {
    if (data.success) {
      ElMessage.success('添加成功')
      addInfo.value = {
        username: '',
        title: '',
        content: '',
      }
      return
    }
    ElMessageBox.alert(data.message, '添加公告')
  })
}

//#endregion

//#region 操作部分 查看内容

// 查看内容对话框中展示的数据
const viewcontent = ref({
  bid: -1,
  username: '',
  title: '',
  content: '',
  topIndex: 1000000,
  lastupdate: '',
})

// 隐藏查看内容的对话框
const viewDisplay = ref(false)

// 获取查看对话框中的数据
const showview = bid => {
  loading.value = true
  api.get(`/crud/billboard/${bid}`, {}, data => {
    loading.value = false
    if (data.success) {
      viewcontent.value = data.data
      viewDisplay.value = true
      return
    }
    ElMessageBox.alert(data.message, '查看内容')
  })
}

//#endregion

//#region 操作部分 删除
const del = info => {
  console.log(info)
  ElMessageBox.confirm(`是否要删除:${info.title}?`, '删除公告')
    .then(() => {
      api.delete(`/crud/billboard/${info.bid}`, {}, data => {
        ElMessage.success(data.message)
        btnSearch()
      })
    })
    .catch(() => {})
}
//#endregion

//#region 操作部分 修改

// 修改需要的数据
const modifyInfo = ref({
  bid: 1,
  username: '',
  title: '',
  content: '',
  topIndex: 1000000,
  lastupdate: '',
})

// 不显示修改的对话框
const modifyDisplay = ref(false)

// 点击修改按钮后
const showModify = bid => {
  loading.value = true
  api.get(`/crud/billboard/${bid}`, {}, data => {
    loading.value = false
    if (data.success) {
      modifyInfo.value = data.data
      modifyDisplay.value = true
      return
    }
    ElMessageBox.alert(data.message, '修改内容')
  })
}

// 修改功能
const modify = () => {
  api.put('/crud/billboard', modifyInfo.value, data => {
    ElMessageBox.alert(data.message, '修改内容')
  })
}

//#endregion
</script>

<style scoped>
/* 查看内容对话框中content部分的样式 */
.viewinfo > div:nth-of-type(2) {
  height: 40vh;
}

/* 查看内容对话框中footer部分的样式 */
.viewinfo > div:nth-of-type(3) {
  font-size: 0.8em;
}

/* 查看内容对话框中图片部分的样式 */
.viewinfo:deep img {
  display: block;
  width: 100%;
  height: auto;
}
</style>
